<?php


namespace app\index\controller;

use think\Controller;
// 给模型User一个别名避免和当前User控制器冲突
use app\index\model\Profile;
use app\index\model\User as UserModel;
use app\index\model\Book;
use app\index\model\Role;

class User extends Controller
{

    public function login()
    {
        return '成功进入login方法';
    }

    public function hello($name = 'xk', $city = '湖南')
    {
        return 'hello,i am' . $name . ',i am from ' . $city;
    }

    // 新增用户数据
//    public function add()
//    {
//        $user = new UserModel();
//        $user->nickname = '流年';
//        $user->email = 'thinkphp@qq.com';
////        $user->birthday = strtotime('1977-03-05');
//        $user->birthday = '1977-03-05'; //因为构造器会自动转换,或者会自动转换
//        if ($user->save()) {
//            return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
//        } else {
//            return $user->getError();
//        }
//    }

// 修改新增用户数据
    public function add()
    {
        //关联模型怎么写入
        $user = new UserModel;
        $user->name = 'thinkphp';
        $user->password = '123456';
        $user->nickname = '流年';
        if ($user->save()) {
            // 写入关联数据
            $profile = new Profile;
            $profile->truename = '刘晨';
            $profile->birthday = '1977-03-05';
            $profile->address = '中国上海';
            $profile->email = 'thinkphp@qq.com';
            $user->profile()->save($profile);
            return '用户新增成功';
        } else {
            return $user->getError();
        }
    }

// 下面的方法也是正确的
    public
    function add2()
    {
        $user['nickname'] = '看云';
        $user['email'] = 'kancloud@qq.com';
        $user['birthday'] = strtotime('2015-04-02');
        if ($result = UserModel::create($user)) {
            return '用户[ ' . $result->nickname . ':' . $result->id . ' ]新增成功';
        } else {
            return '新增出错';
        }
    }

// 批量新增用户数据
    public
    function addList()
    {
        $user = new UserModel;
        $list = [['nickname' => '张三', 'email' => 'zhanghsan@qq.com', 'birthday' => strtotime('1 988-01-15')], ['nickname' => '李四', 'email' => 'lisi@qq.com', 'birthday' => strtotime('1990-0 9-19')],];
        $list2 = [['nickname' => '萧红', 'email' => '546483720@qq.com', 'birthday' => strtotime('2020-01-01')]];
        if ($user->saveAll($list2)) {
            return '用户批量新增成功';
        } else {
            return $user->getError();
        }
    }

    public
    function read($id = '')
    {
        $user = UserModel::get($id);
        echo $user->toArray();
//        echo $user->nickname . '<br/>';
//        echo $user->email . '<br/>';
//        echo $user->birthday . '<br/>';
//        echo $user->status . '<br/>';
//        echo $user->create_time . '<br/>';
//        echo $user->update_time . '<br/>';
        //get的第二个参数就是对关联的模型进行预载入查询提高访问效率
//        $user = UserModel::get($id, 'profile');
//        echo $user->name . '<br/>';
//        echo $user->nickname . '<br/>';
//        echo $user->profile->truename . '<br/>';
//        echo $user->profile->email . '<br/>';
//        return $user->toArray();
    }

    public
    function index()
    {
        $list = UserModel::scope('email,status')->all();
        foreach ($list as $user) {
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo $user->birthday . '<br/>';
            echo $user->status . '<br/>';
            echo '-------------------------------------<br/>';
        }
    }

// 更新用户数据
    public
    function update($id)
    {
        $user['id'] = (int)$id;
        $user['nickname'] = '刘晨';
        $user['email'] = 'liu21st@gmail.com';
        $result = UserModel::update($user);
        return '更新用户成功';
    }

// 删除用户数据
    public
    function delete($id)
    {
        $user = UserModel::get($id);
        if ($user) {
            $user->delete();
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
    }

// 删除用户数据
    /*public function delete($id)
    {
        $result = UserModel::destroy($id);
        if ($result) {
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
    }*/
    public
    function create()
    {
        return view();
    }

    public function addone()
    {
        $user = new UserModel;
        $user->name = 'xhk';
        $user->password = '123456';
        $user->nickname = '流年';
        if ($user->save()) {
            $profile = new Profile();
            $profile->truename = '刘晨';
            $profile->birthday = '1977-03-05';
            $profile->address = '中国上海';
            $profile->email = 'thinkphp@qq.com';
            // 写入用关联方法
            $user->profile()->save($profile);
            return '新增成功';
        } else {
            return $user->getError();
        }
    }

    public function readone($id)
    {
        $user = UserModel::get($id, 'profile');
        echo $user->name;
        echo $user->password;
        echo $user->create_time;
        echo $user->update_time . "<br/>";
        echo "-----------------------------<br/>";
        echo $user->profile->address;
    }

    public function updateone($id)
    {
        $user = UserModel::get($id, 'profile');
        $user->name = 'ThinkPhp';
        if ($user->save()) {
            $user->profile->email = '546483720@qq.com';
            $user->profile->save();
            return '更新成功';
        } else {
            return $this->error('没有这个用户');
        }
    }

    public function deleteone($id)
    {
        $user = UserModel::get($id, 'profile');
        if ($user->delete()) {
            $user->profile->delete();
            return '删除成功';
        } else {
            return $this->error('没有这个用户');
        }
    }

    // 新增书
    public function addbook()
    {
        $user = UserModel::get('1');
        $book = new Book;
        $book->title = 'ThinkPHP5快速入门';
        $book->publish_time = '2016-05-06';
        $user->books()->save($book);
        return '添加Book成功';
    }

    //批量新增书籍
    public function addBookALL()
    {
        $user = UserModel::get(1);
        $books = [
            ['title' => 'ThinkPHP5快速入门', 'publish_time' => '2016-05-06'],
            ['title' => 'ThinkPHP5开发手册', 'publish_time' => '2016-03-06'],
        ];
        $user->books()->saveAll($books);
        return '添加Book成功';
    }

    //一对多关联查询
    public function readBook()
    {
        //查询id=1的所有关联数据
        $user = UserModel::get(1, 'books');
        $books = $user->books;
//        dump($books);
        $user = UserModel::get(1);
        // 获取状态为1的关联数据
        $books = $user->books()->where('status', 1)->select();
        dump($books);
        // 获取作者写的某本书
        $book = $user->books()->getByTitle('ThinkPHP5快速入门');
        dump($book);
    }

    public function updateBook($id)
    {
        $user = UserModel::get($id);
        //必须获得一个对象进行更新
        $book = $user->books()->getByTitle('ThinkPHP5快速入门');
        $book->title = 'ThinkPHP5快速入门111';
        $book->save();
    }

//关联删除 删除部分关联数据：
    public function deletebook($id)
    {
        $user = UserModel::get($id);
        // 删除部分关联数据
        $book = $user->books()->getByTitle('ThinkPHP5快速入门111');
        $book->delete();
    }

    public function deletebookall($id)
    {
        $user = UserModel::get($id);
        // 删除所有的关联数据
        if ($user->delete()) {
            $user->books()->delete();
            return 'delete success';
        } else {
            return $this->error('删除失败');
        }
    }
    //下面是多对多模型代码
    // 关联新增数据
    public function addMany()
    {
        $user = new UserModel();
        $user->name = '多对多关联新增';
        $user->nickname = 'thinkphp';
        if ($user->save()) {
            // 新增用户角色 并自动写入枢纽表
            $user->roles()->save(['name' => 'editor', 'title' => '编辑']);
        }
        return '用户角色新增成功';
    }
}